package com.lagou.biz.client;

import com.lagou.niancheng.biz.api.UserApi;
import com.lagou.niancheng.biz.api.entity.User;
import com.lagou.niancheng.rpc.client.SimpleRpcMethodProxyFactory;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * api对接客户端
 *
 * @author wlz
 * @date 2020/5/27
 */
public class BizClient {

    private UserApi userApi;

    @Before
    public void init() {
        // userApi初始化
        SimpleRpcMethodProxyFactory simpleRpcMethodProxyFactory = new SimpleRpcMethodProxyFactory();
        userApi = simpleRpcMethodProxyFactory.createRpcProxy(UserApi.class);
    }

    /**
     * 添加用户
     */
    @Test
    public void insertUser() {
        userApi.insertUser(new User()
                .setUsername("张三")
                .setAge(24));
        userApi.insertUser(new User()
                .setUsername("李四")
                .setAge(24));
        userApi.insertUser(new User()
                .setUsername("王五")
                .setAge(24));
    }

    /**
     * 更新用户
     */
    @Test
    public void updateUser() {
        String userId = "634dada78c524d0f956278a2762f0abf";
        // 更新用户
        userApi.updateUser(new User()
                .setId(userId)
                .setUsername("李四")
                .setAge(23));
    }

    /**
     * 删除用户
     */
    @Test
    public void deleteUser() {
        String userId = "53d3b09bdac74be0aaa8a240ca1739a1";
        userApi.deleteUser(userId);
    }

    /**
     * 查询用户
     */
    @Test
    public void listUsers() {
        List<User> userList = userApi.listAllUsers();
        userList.forEach(System.out::println);
    }

}
